package com.example.opentiktok;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DouYinAccessibilityOperator extends AccessibilityOperator {
    public static String pkgName = "com.ss.android.ugc.trill";
    private volatile static DouYinAccessibilityOperator instance;

    ExecutorService threadPool = Executors.newSingleThreadExecutor();
    //----- 抖音双击点赞变量名
    private String shortVideoActivityName = "com.ss.android.ugc.aweme.detail.ui.DetailActivity"; //作品界面的路径名


    public static DouYinAccessibilityOperator getInstance() {
        if (instance == null) {
            synchronized (DouYinAccessibilityOperator.class) {
                if (instance == null) {
                    instance = new DouYinAccessibilityOperator();
                }
            }
        }
        return instance;
    }

    @Override
    public void doAction(long orderId, boolean start, String className, int kind, boolean isDouble) {
        LogUtil.d("抖音 ----》 重新开始执行doAction操作  === className = " + className + "   isDouble : " + isDouble + " orderId : " + orderId);
        threadPool.execute(() -> {
            if (mAccessibilityService.getRootInActiveWindow() != null) {

                //
                if (className.equals(shortVideoActivityName)) {
                    LogUtil.d("wdd== 抖音 ----shortVideoActivityName 作品页面》");
                    sendMsg(null, 4, 1000);
                }


            }
        });
    }

    private void sendMsg(AccessibilityNodeInfo info, int what, long delayMillis) {
        Message msg = mHandler.obtainMessage();
        msg.what = what;
        msg.obj = info;
        mHandler.sendMessageDelayed(msg, delayMillis);
    }

    /**
     * 匿名内部类，执行相关点击操作
     */
    final Handler mHandler = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void handleMessage(@NonNull Message msg) {
            LogUtil.d("2222222successEmpty 99999 :  msg.what  " + msg.what);
            AccessibilityNodeInfo item = (AccessibilityNodeInfo) msg.obj;
            if (item != null) {
                item.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
            switch (msg.what) {

                case 4:
                    LogUtil.d("wdd== 执行双击点赞");
                    doubleClick(true);
                    mHandler.sendEmptyMessageDelayed(12, 1500);


                    break;
                case 12:
                    sendBroadcast(true, false, "直播关注完成");

                    break;


            }
        }
    };

    private void sendBroadcast(boolean success, boolean successEmpty, String message) {
        Toast.makeText(mContext, "点赞完成", Toast.LENGTH_LONG);
//        Intent intent1 = new Intent(ACTION_WECHAT_BACK_RECEIVER);
//        intent1.putExtra("success", success);
//        intent1.putExtra("kind", kind);
//        intent1.putExtra("message", message);
//        intent1.putExtra("successEmpty", successEmpty);
//        LogUtil.d("2222222successEmpty 99999 : " + success + "   successEmpty  " + successEmpty);
//        mContext.sendBroadcast(intent1);
//        isSearchFlag = false;
//        followFlag = false;
//        liveBroadcastFlag = false;
//        followIndex = 0;
//        isLogin = false;
//        liveBroadcastEndFlag = false;
//        shortVideoCountTime = 60;
//        liveColseFlag = false;
    }

}
